// pages/ucenter/to-comment/to-comment.js
import Toast from '../../../lib/vant-weapp/toast/toast';
const jeegot = require('../../../utils/jeegot.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderItemList: {},
    files: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '评价',
    })
    let orderId = options.id;
    jeegot.getUserProductCommentListByOrder({orderId: orderId}).then(data =>{
      
      for(let i=0;i<data.length;i++) {
        if(data[i].productCommentList.length<=0) {
          // 初始化
          let comment = {
            score: 5,
            scoreTxt: "非常满意"
          }
          data[i].productCommentList.push(comment)
        }
      }
      
      this.setData({
        orderItemList: data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onSuccess: function(data) {
    let resp = JSON.parse(data.detail.file.res.data);
    if(resp.code == 1) {
      let filePath = resp.data;
      this.data.files.push(filePath)
    }
  },

  onRemove: function(data) {
    if(data.detail.file.res) {
      let resp = JSON.parse(data.detail.file.res.data);
      if(resp.code == 1) {
        let filePath = resp.data;
        for(var i=0; i<this.data.files.length;i++) {
          if(filePath == this.data.files[i]) {
            this.data.files.splice(i, 1)
            break;
          }
        }
      }
    }
  },

  onChange: function(data) {
    
  },

  onComplete: function(data) {
    
  },

  onFail: function(data) {
    
  },

  changeInput: function (e) {
    let index = e.currentTarget.dataset.index;
    let itemIndex = e.currentTarget.dataset.pindex
    let v = e.detail;
    this.setData({
      [`orderItemList[${itemIndex}].productCommentList[${index}].content`]: v
    });
  },
  changeRate: function (e) {
    let index = e.currentTarget.dataset.index;
    let itemIndex = e.currentTarget.dataset.pindex
    let v = e.detail.index;
    let rateTxt = "非常满意";
    if (v == 1) {
      rateTxt = "非常差"
    } else if (v == 2) {
      rateTxt = "差"
    } else if (v == 3) {
      rateTxt = "一般吧"
    } else if (v == 4) {
      rateTxt = "满意"
    }
    this.setData({
      [`orderItemList[${itemIndex}].productCommentList[${index}].score`]: v,
      [`orderItemList[${itemIndex}].productCommentList[${index}].scoreTxt`]: rateTxt
    });

  },
  post: function (e) {
    let index = e.currentTarget.dataset.index;
    let itemIndex = e.currentTarget.dataset.pindex
    let content = this.data.orderItemList[itemIndex].productCommentList[index].content;
    if (!content) {
      Toast("请填写评价")
      return;
    }

    let productId = this.data.orderItemList[index].productId
    let orderId = this.data.orderItemList[index].orderId
    let sku = this.data.orderItemList[index].sku || ""
    let score = this.data.orderItemList[itemIndex].productCommentList[index].score
    jeegot.submitProductComment({
      orderId: orderId,
      productId: productId,
      sku: sku,
      content: content,
      score: score,
      images: this.data.files.toString()
    }).then(data =>{
      Toast("评论成功");
      wx.navigateBack({ delta: 1 })
    }).catch(error =>{
      Toast(error.message);
    })
  }
})